import { request } from '../utils/request.js'
import store from '../store'
import { Toast, Jump } from '../utils/tools.js'
import md5 from 'js-md5'
import { balance } from '@/api/Users.js'



//商品列表
export async function GetGoodsPage(params) {
	try {
		let res = await request({
			url: '/Goods/GetGoodsPage',
			method: "post",
			params
		})

		if (res.success) {
			res.list.forEach(item => {
				item.bannerpicurl = item.picurls.split(',')
				item.picurl = item.bannerpicurl[0]
			})
		}
		return res
	} catch (e) {
		console.log(e)
		return false
	}
}



//商品详情
export async function GoodsFirst(params) {
	try {
		let res = await request({
			url: '/Goods/GoodsFirst',
			method: "post",
			params
		})
		const { success, data } = res
		if (success) {
			const { items, limit } = data
			items.forEach(item => {
				item.bannerpicurl = item.picurls.split(',')
				item.picurl = item.bannerpicurl[0]
				item.content = item.content.toString().replace(/\<img/gi,
					"<img style='width:100%;padding: 0;display: block;margin: 0;'")
			})
		}
		return res
	} catch (e) {
		console.log(e)
		return false
	}
}


//商品分类
export async function GetGoodsTypePage(params) {
	try {
		let res = await request({
			url: '/Goods/GetGoodsTypePage',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}
//获得一级分类
export async function GetGoodsTypePage1(params) {
	try {
		const { success, list } = await request({
			url: '/Goods/GetGoodsTypePage',
			method: "post",
			params: {
				pid: 0,
				pageSize: 1000
			}
		})
		const arr = list.filter(item => {
			return item.positions == '0'
		})
		store.commit('goods/SET_CLASSIFY', arr)
	} catch (e) {
		return false
	}
}
//购物车
export async function GetShopCar(params) {
	try {
		let res = await request({
			url: '/Goods/GetShopCar',
			method: "post",
			isLogin: false,
			params
		})
		if (res.success) {
			res.list.forEach(item => {
				item.picurl = item.picurl.split(',')[0]
				item.goodsName = item.goodsname
				item.checked = false;
			})
			let arr = res.list || [];
			store.commit('goods/SET_CARTLIST', arr)
			store.commit('goods/SET_CARTLENGTH', res.totalCount)
			return res
		}
		store.commit('goods/SET_CARTLENGTH', 0)
		return {}

	} catch (e) {
		return false
	}
}

export async function ShopCar_Add(params) {
	try {
		let res = await request({
			url: '/Goods/ShopCar_Add',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}



//分类详情
export async function GoodsTypeFirst(params) {
	try {
		let res = await request({
			url: '/Goods/GoodsTypeFirst',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}


//删除购物车
export async function ShopCar_Del(data) {
	try {
		let res = await request({
			url: '/Goods/ShopCar_Del',
			method: "post",
			data
		})
		return res
	} catch (e) {
		return false
	}
}

//修改购物车
export async function ShopCar_Edit(params) {
	try {
		let res = await request({
			url: '/Goods/ShopCar_Edit',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}
//获取我的收藏
export async function getMyGoodsCollect(params) {
	try {
		let res = await request({
			url: '/Goods/getMyGoodsCollect',
			method: "post",
			params
		})
		if (res.success) {
			res.list.forEach(item => {
				item.checked = false;
				item.bannerpicurl = item.bannerpicurl.split(',')
				item.picurl = item.bannerpicurl[0]
			})
		}
		return res
	} catch (e) {
		return false
	}
}

//收藏
export async function CollectGoods(params, data) {
	console.log('params===>', params)
	try {
		let res = await request({
			url: '/Goods/CollectGoods',
			method: "post",
			params,
			data,
			toast: false,
		})
		return res
	} catch (e) {
		return false
	}
}

//获取我的收藏
export async function isCollect(params) {
	try {
		let res = await request({
			url: '/Goods/isCollect',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

//商品评论 列表
export async function CommentByGoods(params) {
	try {
		let res = await request({
			url: '/Goods/CommentByGoods',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}
//商品评论 写入
export async function Comment(params) {
	try {
		let res = await request({
			url: '/Goods/Comment/Comment',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

//清空收藏
export async function CollectGoods_Del(data) {
	try {
		let res = await request({
			url: '/Goods/CollectGoods_Del',
			method: "post",
			data
		})
		return res
	} catch (e) {
		return false
	}
}

export async function MallShopInfo(params) {
	try {
		let res = await request({
			url: '/Goods/MallShopInfo',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}